package com.liyiruo.processor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;

import java.util.Map;

/**
 * bean工厂后置处理器
 * @author liyiruo
 * @Description
 * @Date 2023/4/20 下午10:34
 */
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println("bean工厂后置处理器");

        //获取到BeanDefinition对象，然后修改其中的类；
        BeanDefinition tempbean = beanFactory.getBeanDefinition("helloWorld");
        String beanClassName = tempbean.getBeanClassName();
        tempbean.setBeanClassName("com.liyiruo.pojo.Person");

        //自己定义一个BeanDefinition，赋值后放入
        BeanDefinition beanDefinition = new RootBeanDefinition();
        beanDefinition.setBeanClassName("com.liyiruo.pojo.HelloWord2");
        DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) beanFactory;
        //放到Map<String, BeanDefinition> beanDefinitionMap;
        defaultListableBeanFactory.registerBeanDefinition("helloWord2",beanDefinition);

    }
}
